package com.gmics.unifiq.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import org.json.JSONException;
import org.json.JSONObject;

import com.gmics.unifiq.entity.Course;

import android.util.Log;

public class CourseFunctions {

	private JSONParser jsonParser;

	// url to get all products list
	private static String get_course = "http://unifiq.5kportal.com/comparison_api1/getCourse.php";

	// JSON Node names
	private static final String TAG_SUCCESS = "success";
	

	// constructor
	public CourseFunctions() {
		jsonParser = new JSONParser();
	}


	public Course getCourse(String courseName) throws JSONException
	{
		List<NameValuePair> params1 = new ArrayList<NameValuePair>();
		params1.add(new BasicNameValuePair("courseName", courseName));
		JSONObject obj = jsonParser.makeHttpRequest(get_course, "POST", params1);
		
		Course c = null;
		// Check your log cat for JSON reponse
		Log.d(" ", "getCourse(): " + obj.toString());
		
		// Course auto-complete data
		try {
			
			JSONObject j = obj.getJSONObject("course");
			
			// Checking for SUCCESS TAG
			int success = obj.getInt(TAG_SUCCESS);
			
			if (success == 1) {
				
				//Log.d(" ", "courseId: " + j.get("courseId"));
				
				
				c = new Course();
				c.setCourseId(Integer.parseInt((String) j.get("courseId")));
				c.setSchoolId(Integer.parseInt((String) j.get("schoolId")));
				c.setUniName( j.get("uniName").toString());
				c.setCourseName(j.get("courseName").toString());
				
				c.setEntryRecq((String) j.get("entryRecq").toString());
				
				c.setAdminSchedule((String) j.get("adminSchedule"));
				c.setCutOffGpa((String) j.get("cutOffGpa"));
				c.setGpaPerc((String) j.get("gpaPerc"));
				c.setLength((String) j.get("length"));
				c.setScholarship((String) j.get("scholarship"));
				c.setEmail((String) j.get("email"));
				c.setPhone((String) j.get("phone"));
				c.setFees((String) j.get("fees"));
				
			} 
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return c;
	}
	

}
